package math;

import java.util.*;
import java.util.Random;

import org.junit.Test;

public class Ex8 {
    class Solution {
        public int myAtoi(String s) {
            int len = s.length();
            if (len == 0)
                return 0;
            s = s.trim();
            boolean flag = true;
            char f = s.charAt(0);
            if (!checkInt(f)) {
                if (len == 1)
                    return 0;
                if (f == '+') {
                    s = s.substring(1);
                    flag = true;
                } else if (f == '-') {
                    s = s.substring(1);
                    flag = false;
                } else {
                    return 0;
                }
            }
            int start = 0, end = 0;
            boolean st = false;
            len = s.length(); 
            while (end < len && checkInt(s.charAt(end))) {
                if (!st && s.charAt(end) != '0') {
                    st = true;
                    start = end;
                }
                end++;
            }
            if (s.charAt(start) == '0') return 0;
            if (start == end) return 0;
            s = s.substring(start, end);
            int res = 0, idx = 0;
            if (s.length() > 10 || s.length() == 10 && s.charAt(0) > '2')
                return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            len = s.length();
            while (idx < len) {
              /*   res += (s.charAt(len - idx - 1) - '0') * (int) Math.pow(10, idx);
                if (idx == len - 2 && (res > 214748364
                        || res == 214748364 && (flag && s.charAt(0) > 7 || !flag && s.charAt(0) > 8))) {
                    return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                } */
                if (idx == 9 && (res > 214748364
                        || res == 214748364 && (flag && s.charAt(idx) > '7' || !flag && s.charAt(idx) > '8'))) {
                    return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                }
                res = res * 10 + s.charAt(idx) - '0';
                idx++;
            }
            return res * (flag ? 1 : -1);
        }

        public boolean checkInt(char c) {
            return c >= '0' && c <= '9';
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        
        // int res = s.myAtoi("-3147484648");
        // int res = s.myAtoi("48");

       int  res = s.myAtoi("2147483646");

        System.out.println(res);
    }


    @Test
    public void test2() {
        Random r = new Random();
        List<String> head = List.of(" ", "+", "-");
        List<String> pool = new ArrayList<>();
        for (int i = 0; i < 127; i++) {
            char c = (char)i;
            if (c >= '0' && c <= '9' || c >= 'A' && c <= 'z')  {
                pool.add(c + "");
            }
        }

        for (int i = 0; i < 100; i++) {
            String h = "";
            String res = "";
            int h_len = r.nextInt(10), t_len = r.nextInt(50);
            for (int j = 0; j < h_len; j++) {
                h += head.get(r.nextInt(head.size()));
            }
            for (int j = 0; j < t_len; j++) {
                res += pool.get(r.nextInt(pool.size()));
            }
            System.out.println(h + res);
        }
    }
}
